#ifndef __WEBLIB_HPP__
#define __WEBLIB_HPP__

#include "web_page.hpp"

#include <iostream>
#include <unordered_map>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <fstream>
#include <memory>

using std::cout;
using std::endl;
using std::unordered_map;
using std::set;
using std::string;
using std::pair;
using std::ifstream;
using std::stringstream;
using std::shared_ptr;

namespace gcj
{

class WebLib
{
private:
    WebLib() {  }
    ~WebLib() {  }

public:
    //加载库文件
    void loadLibrary();

public:
    //返回指向本类型的指针
    static WebLib * getInstance() {
        if(_pInstance == nullptr) {
            _pInstance = new WebLib();
        }
        return _pInstance;
    }
    unordered_map<long, shared_ptr<WebPage>> getPageLib() {
        return _pageLib;
    }
    unordered_map<long, pair<long, int>> getOffsetLib() {
        return _offsetLib;
    }
    unordered_map<string, set<pair<long, double>>> getInvertIndexTab() {
        return _invertIndexTable;
    }

private:
    static WebLib * _pInstance;
    unordered_map<long, shared_ptr<WebPage>> _pageLib;  //网页库
    unordered_map<long, pair<long, int>> _offsetLib;    //偏移库
    unordered_map<string
        , set<pair<long, double>>> _invertIndexTable;   //倒排索引表

private:
    class AutoRelease
    {
    public:
        AutoRelease() {  }
        ~AutoRelease() {
            //实现单例类的自动回收
            cout << "~AutoRelease()" << endl;
            if(WebLib::_pInstance == nullptr) {
                return;
            }
            delete WebLib::_pInstance;
            WebLib::_pInstance = nullptr;
        }
    };
private:
    static AutoRelease _at;
};

}//end of namespace gcj
#endif
